﻿using Koala.Pro.Common;
using Koala.Pro.Core;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using SqlSugar;

namespace Koala.Pro.Services
{
    public class OperationLogSettingService : MyCrudService<OperationLogSettingInfo, string, OperationLogSettingPagedDto>, IOperationLogSettingService, IMyCrudService<OperationLogSettingInfo, string, OperationLogSettingPagedDto>, ITransientDependency, IDependency
    {
        public OperationLogSettingService(IApiUserSession currentApiUser)
        {
            base.CurrentApiUser = currentApiUser;
        }

        public override Task<Dictionary<string, string>> GetColumnNameAlias()
        {
            return Task.FromResult(new Dictionary<string, string>
        {
            { "Forbid", "是否禁用" },
            { "TableName", "表名称/实体类全名" },
            { "InsertLog", "记录插入日志" },
            { "DeleteLog", "记录删除日志" },
            { "UpdateLog", "记录更新日志" },
            { "Note", "备注" },
            { "Creator", "创建人" },
            { "Creator_ID", "创建人ID" },
            { "CreateTime", "创建时间" },
            { "Editor", "编辑人" },
            { "Editor_ID", "编辑人ID" },
            { "EditTime", "编辑时间" }
        });
        }

        protected override ISugarQueryable<OperationLogSettingInfo> CreateFilteredQueryAsync(OperationLogSettingPagedDto input)
        {
            return base.CreateFilteredQueryAsync(input).WhereIF(!input.ExcludeId.IsNullOrWhiteSpace(), (OperationLogSettingInfo t) => t.Id != input.ExcludeId).WhereIF(input.Forbid.HasValue, (OperationLogSettingInfo s) => s.Forbid >= (int?)input.Forbid.Value)
                .WhereIF(!input.TableName.IsNullOrWhiteSpace(), (OperationLogSettingInfo t) => t.TableName.Contains(input.TableName))
                .WhereIF(input.InsertLog.HasValue, (OperationLogSettingInfo s) => s.InsertLog >= (int?)input.InsertLog.Value)
                .WhereIF(input.DeleteLog.HasValue, (OperationLogSettingInfo s) => s.DeleteLog >= (int?)input.DeleteLog.Value)
                .WhereIF(input.UpdateLog.HasValue, (OperationLogSettingInfo s) => s.UpdateLog >= (int?)input.UpdateLog.Value)
                .WhereIF(!input.Note.IsNullOrWhiteSpace(), (OperationLogSettingInfo t) => t.Note.Contains(input.Note))
                .WhereIF(!input.Creator.IsNullOrWhiteSpace(), (OperationLogSettingInfo t) => t.Creator.Contains(input.Creator))
                .WhereIF(!input.Creator_ID.IsNullOrWhiteSpace(), (OperationLogSettingInfo t) => t.Creator_ID.Contains(input.Creator_ID))
                .WhereIF(input.CreateTimeStart.HasValue, (OperationLogSettingInfo s) => s.CreateTime >= input.CreateTimeStart.Value)
                .WhereIF(input.CreateTimeEnd.HasValue, (OperationLogSettingInfo s) => s.CreateTime <= input.CreateTimeEnd.Value)
                .WhereIF(!input.Editor.IsNullOrWhiteSpace(), (OperationLogSettingInfo t) => t.Editor.Contains(input.Editor))
                .WhereIF(!input.Editor_ID.IsNullOrWhiteSpace(), (OperationLogSettingInfo t) => t.Editor_ID.Contains(input.Editor_ID))
                .WhereIF(input.EditTimeStart.HasValue, (OperationLogSettingInfo s) => s.EditTime >= input.EditTimeStart.Value)
                .WhereIF(input.EditTimeEnd.HasValue, (OperationLogSettingInfo s) => s.EditTime <= input.EditTimeEnd.Value);
        }

        protected override ISugarQueryable<OperationLogSettingInfo> ApplyDefaultSorting(ISugarQueryable<OperationLogSettingInfo> query)
        {
            return from s in query
                   orderby s.CreateTime
                   orderby s.TableName
                   select s;
        }

        public async Task<bool> IsTableNeedtoLog(string tablename)
        {
            return await IsExistAsync((OperationLogSettingInfo s) => s.TableName == tablename && s.Forbid == (int?)0);
        }

        public async Task<OperationLogSettingInfo> FindByTableName(string tablename)
        {
            return await GetFirstAsync((OperationLogSettingInfo s) => s.TableName == tablename && s.Forbid == (int?)0);
        }
    }
}
